package com.itheima.Service.CheckServicelmpl;

import com.itheima.Service.OrderSetService;
import com.itheima.common.utils.POIUtils;
import com.itheima.mapper.OrderSetMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrdersetDEV;
import com.itheima.pojo.OrdersetTO;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderSetServicelmpl implements OrderSetService {
    @Autowired
    private OrderSetMapper orderSetMapper;

    /**
     * 模板下载
     * @param filename
     */
    public void DOwlodfile(String filename, HttpServletResponse response) {
        //实现模板下载
        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Template");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"日期", "预约人数" };
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 配置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);

        // 将工作簿写入响应输出流
        try (OutputStream outputStream = response.getOutputStream()) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭工作簿
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 修改可预约人数
     * @param ordersetDEV
     */
    public void upadte(OrdersetDEV ordersetDEV) {
        orderSetMapper.update(ordersetDEV);
    }

    /**
     * 查询预约信息
     * @param month
     * @return
     */
    public List<OrdersetTO> list(String month) {
        List<OrderSetting> QRR=orderSetMapper.list(month);
        List<OrdersetTO> ARR=new ArrayList<>();
        for (OrderSetting orderSetting : QRR) {
            OrdersetTO ordersetTO = new OrdersetTO();
            BeanUtils.copyProperties(orderSetting,ordersetTO);
            String s = orderSetting.getOrderDate().toString();
            String substring = s.substring(8,10);
            ordersetTO.setDate(substring);
            ARR.add(ordersetTO);
        }
        return ARR;
    }

    /**
     * 文件上传
     * @param excelFile
     */
    public void insert(MultipartFile excelFile) {
        try {
            //读入excle文件
            List<String[]> strings = POIUtils.readExcel(excelFile);
            for (String[] string : strings) {
               orderSetMapper.insert(string[0],string[1],string[3]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

